package com.cjk.console.mqtt;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.cjk.console.enums.EventTopic;
import com.cjk.console.enums.LogTopic;
import com.cjk.console.model.Callback;
import com.cjk.console.model.DeviceInfo;
import com.cjk.console.service.DeviceService;
import com.cjk.console.service.LogService;
import com.cjk.console.service.TunnelService;
import com.cjk.foundation.entity.FlowLogEntity;
import com.cjk.foundation.entity.SysInfoLogEntity;
import com.cjk.foundation.util.SpringTool;

@Component
public class MqttPushCallback implements MqttCallback {

	private static final Logger LOGGER = LoggerFactory.getLogger(MqttPushCallback.class);

	@Override
	public void connectionLost(Throwable throwable) {
		LOGGER.error("server is lost connection");
		throwable.printStackTrace();
		new MqttSubscribeClient();
	}

	@Override
	public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
		if (iMqttDeliveryToken.isComplete()) {
			LOGGER.info("message is delivery complete");
		}
	}

	@Override
	public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
		LogService logService = (LogService) SpringTool.getBean(LogService.class);
		DeviceService deviceService = (DeviceService) SpringTool.getBean(DeviceService.class);
		final String message = new String(mqttMessage.getPayload());
		LOGGER.info("message : " + message);
		if (LogTopic.getEnumStringList().contains(topic)) {
			LogTopic logTopic = LogTopic.valueOf(topic);
			switch (logTopic) {
			case INFORM:
				DeviceInfo deviceInfo = JSONObject.parseObject(message, DeviceInfo.class);
				deviceService.updateDeviceByInform(deviceInfo);
				break;
			case SYSINFO_LOG:
				SysInfoLogEntity sysInfoLogEntity = JSONObject.parseObject(message, SysInfoLogEntity.class);
				logService.insertSysInfoLog(sysInfoLogEntity);
				break;
			case FLOW_LOG:
				FlowLogEntity flowLogEntity = JSONObject.parseObject(message, FlowLogEntity.class);
				logService.insertFlowLog(flowLogEntity);
				break;
			}
			return;
		}
		if (EventTopic.getEnumStringList().contains(topic)) {
			EventTopic eventTopic = EventTopic.valueOf(topic);
			Callback callback = JSONObject.parseObject(message, Callback.class);
			switch (eventTopic) {
			case REBOOT_CALLBACK:
				break;
			case CONFIG_TV_CALLBACK:
				deviceService.configTvCallBack(callback);
				break;
			case CONFIG_DEVICE_CALLBACK:
				break;
			case CONFIG_AD_CALLBACK:
				break;
			case CREATE_TUNNEL_CALLBACK :
				TunnelService tunnelService = (TunnelService) SpringTool.getBean(TunnelService.class);
				tunnelService.createComplete(callback.getTunnelId(), callback.getResponse().get(0).getLines().get(0));
				break;
			}
			return;
		}
		LOGGER.error("unkonw topic : {}, message is : {}", topic, message);
	}
	
}
